//
//  ContentView.swift
//  xcodesort
//
//  Created by 赵梁 on 2021/5/26.
//

import SwiftUI

struct ItemView: View {
    
    var title: String
    var text: Binding<String>
    var placehodler: String?
    
    var body: some View {
        HStack {
            Text(title).frame(width: 50)
            TextField(placehodler ?? title, text: text)
        }.padding(.horizontal, 20)
    }
}

struct ContentView: View {
    
    
    @State var projDir: String = ""
    @State var projectName: String = ""
    @State var targetName: String = ""
    
    @State var log: String = ""
    
    @State var appproject: VWKProject?

    
    var body: some View {
        ScrollView {
            Text("iOS工程UML")
                .padding()
            
            TextField("请填入工程根路径", text: $projDir) { editing in
                print("编辑 \(editing)")
            } onCommit: {
                print("填写完成")
            }
            Button("确定") {
                
            }
            ItemView(title: "Path", text: $projDir)
            ItemView(title: "project", text: $projectName)
            ItemView(title: "target", text: $targetName)
            
            HStack {
                Button("检测") {
                    checkProj()
                }
                
                Button("导出ObjcUML") {
                    guard let _ = appproject else {
                        return
                    }
                    
                    KSHObjcUML.sharedPlugin().startGenerateObjcGraph()
                }
                Button("导出SwiftUML") {
                    guard let _ = appproject else {
                        return
                    }
                    KSHObjcUML.sharedPlugin().startGenerateSwiftGraph()
                }
            }
            
            Text(log).padding(.horizontal, 10)
        }
    }
    
    private func checkProj() {
        print("检测工程。。。")
        // 生成Project
        // 检测-workspace
        
        log = ""
//        self.appproject =
        guard let project = VWKProject(name: projectName, path: projDir, originalName: targetName) else {
            log = "生成VWKProject失败"
            KSHObjcUML.sharedPlugin().project = nil
            return
        }
        
        appproject = project
        KSHObjcUML.sharedPlugin().project = project
        
        log = "生成VWKProject成功"
        
        log += "\n hasDirectoryPath \(project.hasDirectoryPath())"
        log += "\n hasXcodeproj \(project.hasXcodeproj())"
        log += "\n hasXcworkspace \(project.hasXcworkspace())"
        log += "\n hasPodfile \(project.hasPodfile())"
        log += "\n hasPodspecFile \(project.hasPodspecFile())"
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(projDir: "/Users/zhaoliang/工作台/code-station/company-code/mb-code/repos/HCBTruckNavi的副本/Example", projectName: "HCBTruckNavi-Example", targetName: "HCBTruckNavi-Example")
    }
}
